define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ykjp/sell/sellaudit/index' + location.search,
                    add_url: 'ykjp/sell/sellaudit/add',
                    edit_url: 'ykjp/sell/sellaudit/edit',
                    del_url: 'ykjp/sell/sellaudit/del',
                    multi_url: 'ykjp/sell/sellaudit/multi',
                    details_url: "ykjp/sell/sellaudit/detail",
                    audit_url1: "ykjp/sell/sellaudit/audit?act=",
                    table: 'ykjp_sell',
                    audit_url: 'ykjp/sell/sellaudit/audit?ids=',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                            checkbox: true,
                            formatter: function (value, row, index) {
                                // console.log(row.status)
                                if (row.status_list == 1) {
                                    return {
                                        disabled: false,
                                    };
                                } else {
                                    return {
                                        disabled: true,
                                    }
                                }
                            }
                        },
                        {
                            field: 'code',
                            title: __('Code')
                        },
                        {
                            field: 'billing_time',
                            title: __('Billing_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD"
                        },
                        {
                            field: 'customer_name',
                            title: __('Customer_name')
                        },
                        {
                            field: 'real_money',
                            title: __('Real_money'),
                            operate: 'BETWEEN'
                        },
                        {
                            field: 'status_list',
                            title: __('Status_list'),
                            searchList: {
                                "-1": __('Status_list -1'),
                                "1": __('Status_list 1'),
                                "2": __('Status_list 2'),
                                "3": __('Status_list 3'),
                                "4": __('Status_list 4'),
                                "5": __('Status_list 5'),
                                "6": __('Status_list 6'),
                                "7": __('Status_list 7'),
                                "8": __('Status_list 8'),
                                "9": __('Status_list 9'),
                                "10": __('Status_list 10')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        // {field: 'id', title: __('Id')},
                        // {field: 'code', title: __('Code')},
                        // {field: 'customer_info_id', title: __('Customer_info_id')},
                        // {field: 'customer_name', title: __('Customer_name')},
                        // {field: 'linkman', title: __('Linkman')},
                        // {field: 'phone', title: __('Phone')},
                        // {field: 'delivery_time', title: __('Delivery_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        // {field: 'purman_id', title: __('Purman_id')},
                        // {field: 'billing_time', title: __('Billing_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'city', title: __('City')},
                        // {field: 'address', title: __('Address')},
                        // {field: 'total_nums', title: __('Total_nums')},
                        // {field: 'rate', title: __('Rate'), operate:'BETWEEN'},
                        // {field: 'theory_money', title: __('Theory_money'), operate:'BETWEEN'},
                        // {field: 'real_money', title: __('Real_money'), operate:'BETWEEN'},
                        // {field: 'status_list', title: __('Status_list'), searchList: {"1":__('Status_list 1'),"2":__('Status_list 2'),"3":__('Status_list 3'),"4":__('Status_list 4'),"5":__('Status_list 5'),"6":__('Status_list 6')}, formatter: Table.api.formatter.normal},
                        // {field: 'firmid', title: __('Firmid')},
                        // {field: 'admin_id', title: __('Admin_id')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                    name: 'addtabs',
                                    title: __('look'),
                                    classname: 'btn btn-xs btn-primary  btn-addtabs',
                                    icon: 'fa fa-pencil-square-o',
                                    url: $.fn.bootstrapTable.defaults.extend.details_url,
                                }, {
                                    name: 'yes',
                                    title: __('Approved yes'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    url: $.fn.bootstrapTable.defaults.extend.audit_url1 + 'pass',
                                    hidden: function (row) {
                                        if (row.status_list == 1) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        layer.msg('提交成功', {
                                            time: 2000, //2s后自动关闭
                                            end: function () {
                                                location.reload();
                                            }
                                        })
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }, {
                                    name: 'no',
                                    title: __('Approved no'),
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-exclamation-triangle',
                                    hidden: function (row) {
                                        if (row.status_list == 1) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    click: function (data, row) {
                                        appWhy(row.id);
                                    }
                                }, ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            //批量通过审核
            $(document).on("click", ".btn-selected", function () {
                var checked = table.bootstrapTable('getSelections');

                var arr = new Array();
                for (var i = 0; i < checked.length; i++) {
                    arr.push(checked[i].id);
                }
                Fast.api.ajax({
                    url: $.fn.bootstrapTable.defaults.extend.audit_url + arr,
                    data: {
                        "act": "pass"
                    }
                }, function (data, ret) {
                    //成功的回调

                    if (data.result = "success") {

                        layer.msg('成功提交', {
                            time: 2000, //2s后自动关闭
                            end: function () {
                                location.reload();
                            }
                        });
                    }
                }, function (data, ret) {
                    return false;
                });
            });

            //批量驳回审批
            $(document).on("click", ".btn-selected-no", function () {
                Layer.confirm("确认选中行均未通过审批？", function () {
                    var checked = table.bootstrapTable('getSelections');
                    var arr = new Array();
                    for (var i = 0; i < checked.length; i++) {
                        arr.push(checked[i].id);
                    }
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.audit_url + arr,
                        data: {
                            "act": "reject"
                        }
                    }, function (data, ret) {
                        //成功的回调
                        if (data.result = "success") {

                            layer.msg('成功提交', {
                                time: 2000, //2s后自动关闭
                                end: function () {
                                    location.reload();
                                }
                            });
                        }
                    }, function (data, ret) {
                        return false;
                    });
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'ykjp/sell/sellaudit/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'ykjp/sell/sellaudit/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'ykjp/sell/sellaudit/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

//填写驳回审核理由
function appWhy(id) {
    var str = "<div  style='width:80%;margin-left:auto;margin-right:auto'><textarea class='reason' rows='3' cols='38'></textarea></div>";
    Layer.open({
        title: __('Please fill in the reason'), //页面标题
        content: str,
        area: ['400px', '230px'], //自定义文本域宽高
        btn: ['确定', '取消'],
        yes: function (e, value) {
            var reason = $(".reason").val().trim();
            if (reason == "") {
                alert(__("Reason cannot be empty"));
            } else {
                apprNo(reason, id);
            }
        },
        btn2: function () {
            return 0;
        }
    })
}

//提交驳回
function apprNo(why, id) {
    Fast.api.ajax({
        url: "ykjp/sell/sellaudit/audit?ids=" + id,
        data: {
            "act": "reject",
            "cause": why
        }
    }, function (data, ret) {
        //成功的回调
        if (data.result = "success") {

            layer.msg('成功提交', {
                time: 2000, //2s后自动关闭
                end: function () {
                    location.reload();
                }
            });
        }
    }, function (data, ret) {
        Layer.alert(ret.msg);
        return false;
    });
}

//打印
// jQuery('body').delegate('#prind', 'click', function () {
//     var selector = Boolean(jQuery('body').find(".prindBody")) ? jQuery('body').find(".prindBody") : jQuery('body').find(".prindBody");
//     jQuery(selector).print({
//         addGlobalStyles: true,
//         stylesheet: '/assets/css/print.css',
//         rejectWindow: true,
//         noPrintSelector: ".no-print",
//         iframe: true,
//         append: null,
//         prepend: '<div style="text-align: center;font-size: 25px;padding-bottom:20px">销售单 </div>'
//     });
//     return false;
// });